﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;
using System.Web.Security;
public partial class admin_EntryList : SitePage
{
    #region ViewState
    public int count = 0;


    busStoreOrderDetail _busOrderDetail = new busStoreOrderDetail();
    busEntry _busEntry = new busEntry();
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    public int OrderDetailId
    {

        get
        {

            object o = ViewState["OrderDetailId"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["OrderDetailId"] = value; }

    }
    public int OrderId
    {

        get
        {

            object o = ViewState["OrderId"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["OrderId"] = value; }

    }
    public int LocationId
    {

        get
        {

            object o = ViewState["LocationId"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["LocationId"] = value; }

    }
    public int F_PageIndex
    {

        get
        {

            object o = ViewState["F_PageIndex"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageIndex"] = value; }

    }
    public int F_PageSize
    {

        get
        {

            object o = ViewState["F_PageSize"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageSize"] = value; }

    }

    public string F_SortField
    {

        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortField"] = value; }

    }
    public string F_SortExpression
    {

        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortExpression"] = value; }

    }

    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            this.OrderDetailId = clsQueryString.GetInt("OrderDetailId");
            this.LocationId = clsQueryString.GetInt("LocationId");
            GetParamater();
            LoadDataOrderDetail();
            LoadData();
        }
        //  Response.Write("Application:" + this.ApplicationName);
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.F_PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.F_PageIndex = 1;
        this.F_PageSize = Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Entry objData = (Entry)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.EntryId.ToString() + ";";

            Literal ltr = (Literal)e.Item.FindControl("ltrQuantity");
            if (ltr != null)
            {
                if (objData.Quantity > 0)
                    ltr.Text = objData.Quantity.ToString("###,###,###");
                else ltr.Text = "0";
            }
            ltr = (Literal)e.Item.FindControl("ltrProducedDate");
            if (ltr != null)
            {
                if (objData.ProducedDate != DateTime.MinValue)
                    ltr.Text = clsConvert.String(objData.ProducedDate);
                else ltr.Text = "-";
            }
            ltr = (Literal)e.Item.FindControl("ltrLocation");
            if (ltr != null)
            {
                if (objData.LocationId > 0)
                {
                    busLocation _busLocation = new busLocation();
                    Location objLocation = _busLocation.GetByID(objData.LocationId);
                    if (objLocation != null)
                        ltr.Text = objLocation.Name;
                }
            }
            ltr = (Literal)e.Item.FindControl("ltrProduced");
            if (ltr != null)
            {
                if (objData.ProducerId != Guid.Empty)
                {
                    MembershipUser user = Membership.GetUser(objData.ProducerId);
                    if (user != null)
                    {
                        ltr.Text = user.UserName;
                    }
                }
            }
            ltr = (Literal)e.Item.FindControl("ltrShift");
            if (ltr != null)
            {
                if (objData.ShiftId > 0)
                {
                    clsSQL sql = new clsSQL();
                    string Shift = (string)sql.ExecuteScalar("Select Name from Shift where ShiftId=" + objData.ShiftId.ToString());
                    ltr.Text = Shift;
                }
            }

            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.EntryId.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if (e.Item.ItemIndex % 2 == 0)
                row.Attributes.Add("class", "alt-row");

        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {


                case "Delete":
                    {
                        if (Delete(id))
                        {
                            LoadDataOrderDetail();
                            LoadData();
                        }
                        break;
                    }
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderDetail.aspx?OrderId=" + this.OrderId);
    }
    protected void btnCancel1_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderDetail.aspx?OrderId=" + this.OrderId);
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.F_SortField = busStoreOrder.OrderId;
        this.F_SortExpression = "ASC";
        this.F_PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Delete(clsConvert.Int(arr[i].ToString()));
            }
        }
        LoadDataOrderDetail();
        LoadData();

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("EntryAdd.aspx?OrderDetailId=" + this.OrderDetailId + "&LocationId=" + this.LocationId);
    }

    #endregion

    #region Methods


    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }


    private bool Delete(int id)
    {

        return _busEntry.Delete(id);
    }
    private void LoadDataOrderDetail()
    {
        StoreOrderDetail objOrder = _busOrderDetail.GetByID(this.OrderDetailId);
        if (objOrder != null)
        {
            btnAddNew.Visible = true;
            txtDesignCode.Text = objOrder.DesignCode;
            txtRequestQuantity.Text = objOrder.Quantity.ToString("###,###,###");
            clsSQL sql = new clsSQL();
            int count = (int)sql.ExecuteScalar("Select COALESCE(sum(Quantity),0) from Entry where OrderDetailId=" + OrderDetailId.ToString() + " and LocationId=" + this.LocationId);
            if (count > 0)
            {
                txtQuantityFinish.Text = count.ToString("###,###,###");
                if (this.LocationId == WebUtil.Common.LocationThietKe)
                    btnAddNew.Visible = false;
                else if (count >= objOrder.Quantity)
                    btnAddNew.Visible = false;
            }
            else txtQuantityFinish.Text = "0";

            int available = objOrder.Quantity - count;
            if (available <= 0)
                txtQuantity.Text = "0";
            else txtQuantity.Text = available.ToString("###,###,###");
            this.OrderId = objOrder.OrderId;
        }
    }
    int total = 0;
    private void LoadData()
    {
        // return;
        paraURL = GetPageParams(FilterFieldType.All);
        hidID.Value = string.Empty;
        //get Condition
        Entry objSearch = new Entry();
        objSearch.CurrentPage = this.F_PageIndex;
        objSearch.PageSize = this.F_PageSize;
        objSearch.OrderBy = this.F_SortField;
        objSearch.OrderDirection = this.F_SortExpression;
        objSearch.Condition = " OrderDetailId=" + this.OrderDetailId + " and LocationId=" + this.LocationId;
        EntryCollection lstResult = _busEntry.List(objSearch);
        total = objSearch.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
            btnAddNew1.Visible = false;
            btnCancel1.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
            btnAddNew1.Visible = true;
            btnCancel1.Visible = true;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();
        Pager1.SetPaging(this.F_PageIndex, count, total);
        Pager2.SetPaging(this.F_PageIndex, count, total);
    }
    private void GetParamater()
    {
        this.OrderDetailId = clsQueryString.GetInt("OrderDetailId");
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = "CreatedDate";
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        F_PageIndex = clsQueryString.GetInt("F_PageIndex");
        F_PageSize = clsQueryString.GetInt("F_PageSize");
        if (F_PageSize < 1)
            F_PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        if (F_PageIndex < 1)
            F_PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = F_PageSize;

    }
    #endregion
}